package servlet;

import entity.User;
import service.BookService;
import service.LendService;
import service.impl.LendServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "BorrowBooksServlet", value = "/BorrowBooksServlet")
public class BorrowBooksServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String bookId=request.getParameter("bookId");
        User user=(User) request.getSession().getAttribute("user");
        String userId=user==null?"null":user.getUserId();
        LendService lendService=new LendServiceImpl();
        if(lendService.borowBook(userId,bookId)){
            response.getWriter().print("借阅成功！1s后返回！");
            response.setHeader("refresh","1;url=AddRecordServlet");
        }else{
            response.getWriter().print("借阅失败！1s后返回！");
            response.setHeader("refresh","1;url=AddRecordServlet");
        }
    }
}
